﻿#region Summary

// 
// Owner : JackieHan 
// 
// Solution : Erp 
// 
// Project : Jackie.Utility.BvConversion
// 
// File : DefaultPropertyDefineSet.cs
// 
// CopyRight : Copyright © 2010-2011 Skomart Corporation, All Rights Reserved 
// 
// Summary : 默认属性定义设置
// 
// History : 
//			2012-07-13 17:17:20	    Create 
// 

#endregion

#region Using

using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using Jackie.Utility.CustomExtension;

#endregion

namespace Jackie.Utility.BvConversion
{
    ///<summary>
    ///默认属性定义设置
    ///</summary>
    public class DefaultPropertyDefineSet<TB, TV> : IPropertyDefineSet<TB, TV>
        where TB : class
        where TV : class
    {
        private ObjectConversionConfig objectConfig;

        /// <summary>
        /// 
        /// </summary>
        /// <param name="objectConfig"></param>
        public DefaultPropertyDefineSet(ObjectConversionConfig objectConfig)
        {
            this.objectConfig = objectConfig;
        }

        #region Implementation of IPropertyDefineSet<TB,TV>

        /// <summary>
        /// 
        /// </summary>
        /// <param name="bProperty"></param>
        /// <param name="vProperty"></param>
        /// <param name="isKey"></param>
        /// <param name="isSubKey"></param>
        public void Set(Expression<Func<TB, object>> bProperty, Expression<Func<TV, object>> vProperty, bool isKey, bool isSubKey)
        {
            var bMembers = TypeExtension.DecodeMembersAccessExpression(bProperty);
            var vMember = TypeExtension.DecodeMemberAccessExpression(vProperty);

            var bPropertyName = string.Empty;
            foreach (var member in bMembers)
            {
                if (string.IsNullOrEmpty(bPropertyName))
                    bPropertyName += member.Name;
                else
                    bPropertyName += "." + member.Name;
            }

            var vPropertyName = vMember.Name;

            var propertyConfig = new PropertyConfig()
                                     {
                                         BusinessProperty = bPropertyName,
                                         ViewProperty = vPropertyName,
                                         IsKey = isKey,
                                         IsSubKey = isSubKey,
                                     };
            objectConfig.PropertyConfigs.Add(propertyConfig);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="bProperty"></param>
        /// <param name="vProperty"></param>
        /// <param name="isSubKey"></param>
        public void Set(Expression<Func<TB, object>> bProperty, Expression<Func<TV, object>> vProperty, bool isSubKey)
        {
            Set(bProperty, vProperty, false, isSubKey);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="bProperty"></param>
        /// <param name="vProperty"></param>
        public void Set(Expression<Func<TB, object>> bProperty, Expression<Func<TV, object>> vProperty)
        {
            Set(bProperty, vProperty, false, false);
        }

        #endregion
    }
}